home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3272 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  4.7 KB

  1. Path: Rezonet.net!news
  2. From: ray@ultimate-tech.com (Ray Dunn)
  3. Newsgroups: comp.lang.c
  4. Subject: Another good reason for using static
  5. Date: 25 Jan 1996 02:22:57 GMT
  6. Organization: Ultimate Technographics Inc.
  7. Message-ID: <4e6pi1$dvl@ns.RezoNet.NET>
  8. NNTP-Posting-Host: 204.19.230.7
  9. Mime-Version: 1.0
  10. Content-Type: Text/Plain; charset=US-ASCII
  11. X-Newsreader: WinVN 0.99.7
  12.  
  13. Although this is not strictly a language issue, but a linker issue, 
  14. it appears to not be system specific, so I hope the topic is relevant 
  15. here.
  16.  
  17. After the recent thread on the proper use of static and extern, I 
  18. received email from someone who alluded to something I hadn't paid 
  19. serious attention to before (sorry, there was a server crash which 
  20. lost the mail before I could reply).
  21.  
  22. Programmers of multi-module programs must be aware of *linkage* 
  23. issues, as well as *scope* issues, because identifiers declared 
  24. in different scopes can be made to refer to the same identifier at 
  25. link time.
  26.  
  27. If two modules of a program declare the same variable, say "int x;" 
  28. with file scope, and don't declare at least one of them as "static", 
  29. then in most systems I know, these identifiers have external linkage, 
  30. i.e. the linker will usually resolve "x" to the same address in both 
  31. modules, even though neither module uses the extern storage-class 
  32. specifier.  This is exactly the same mechanism that causes the linker 
  33. to complain of a multiple definition of a function if you use the 
  34. same function name in two modules and don't declare at least one as 
  35. "static", but in the case of non-function identifiers, the linker 
  36. doesn't consider it a multiple definition.
  37.  
  38. Most linkers don't carry type information along with the identifiers 
  39. they are trying to resolve, only their names, so that even if one 
  40. module declares "int x;" and the other module declares "long x;", the 
  41. linker is not aware of any difference, and still resolves both x's to 
  42. the same address, with obvious catastophic results.
  43.  
  44. This can, and should, be overcome in most programs by the discipline 
  45. of declaring all relevant identifiers as static, so they have 
  46. internal linkage, and using an extern declaration in a header file 
  47. for all identifiers required to have external linkage, to ensure that 
  48. all modules use the identifiers consistently.
  49.  
  50. There are situations, though, where it would seem to be quite 
  51. difficult to ensure that these problems are detected.
  52.  
  53. I'm currently working on a large program which consists of two very 
  54. separate pieces of functionality.  There is a front-end (the user 
  55. interface) and a back-end (the processing engine).  Each of these 
  56. consists of many program modules, which are essentially independant 
  57. of all the modules in the other set, and there is a set of bridge 
  58. modules, which contain references to functions and variables on each 
  59. side.
  60.  
  61. All of the module objects are linked together, the front-end, the 
  62. bridge, and the back-end, so that the linker is globally trying to 
  63. resolve all identifiers with external-linkage at the same time.
  64.  
  65. Here's the problem.
  66.  
  67. The back-end code is written by a separate group, is known to be 
  68. "global heavy", haphazard in its use of include files,  and the 
  69. development is outside my control (Real-Life-Management (tm)). If the 
  70. writers of that module have chosen any identifiers with the same 
  71. names as any externs I'm using, I'm in trouble, and although I can't 
  72. be responsible for the product as a whole, I would like to minimize 
  73. problems under my control!
  74.  
  75. If the back-end was written by a group who, for the usual commercial 
  76. reasons did not want to publish their sources, this would be much 
  77. more serious.  Fortunately, in this case, I do have access to the 
  78. sources, and always recompile them including the header files of the 
  79. front-end and bridge modules, which consistently declare all 
  80. externally linked identifiers.  This allows me to detect if the 
  81. back-end is using any of the front-end names but with different 
  82. types, but it still doesn't detect if the back-end is using the same 
  83. identifiers but for different purposes that would cause interference 
  84. with the front-end.
  85.  
  86. The problem is minimized by the use of hopefully unique names in the 
  87. front-end, and I'm currently attempting to totally resolve the 
  88. problem by isolating the back-end into a DLL, but there are bridging 
  89. issues.
  90.  
  91. Although I'm stuck with a specific development environment, does 
  92. anyone know of *any* system that would allow a "fire-wall" to be 
  93. erected between these two modules, given that the bridge between them 
  94. must also exist, or of any linker that ensures uniqueness between 
  95. identifers in different scopes even if not declared static?
  96. -- 
  97. Ray Dunn (opinions are my own) | Phone: (514) 938 9050
  98. Montreal                       | Phax : (514) 938 5225
  99. ray@ultimate-tech.com          | Home : (514) 630 3749
  100.  
  101.